<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/21
 * Time: 9:17
 */

namespace App\GraphQL\Query;

use App\User;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Query;

class UsersQuery extends Query
{

    protected $attributes = [
        'name' => 'users'
    ];

    public function type(): Type
    {
        return Type::listOf(\GraphQL::type('users'));
    }

    /**
     * 接收参数的类型定义
     * @return array
     */
    public function args() :array
    {
        return [
            'id' => ['name' => 'id', 'type' => Type::int()],
            'email' => ['name' => 'email', 'type' => Type::string()],
//            'limit' => ['name' => 'limit', 'type' => Type::int()],
            'name' => [
                'name' => 'name',
                'type' => Type::string()
            ]

        ];
    }

    /**
     * @param $root
     * @param $args 传入参数
     *
     * 处理请求的逻辑
     * @return mixed
     */
    public function resolve($root, $args)
    {
        //测试代码  users 就是本垒 $attributes的name
        $qry = <<<TEST
            query {
              users{
                id
                email
                job{
                  name
                }
              }
            }
            
            或者加参数
            query {
            users(name: "wubuze"){
                id
                email
              }
            }
TEST;

        $qry = User::query();

        if(isset($args['name']) ) {
            $qry->where('name', 'like', '%'.$args['name'].'%');
        }

        if(isset($args['id']))
        {
            $qry->where('id' , $args['id']);
        }

        if(isset($args['email']))
        {
            $qry->where('email', $args['email']);
        }

        return $qry->get();
    }



}
